#compdef mkdocs
########################################################################################
# -*- mode:sh; -*-
# Time-stamp: <Thu 2017-02-02 15:36 svarrette>
#
# Zsh completion for [MkDocs](http://www.mkdocs.org/)
#
# Copyright (c) 2017 Sebastien Varrette <Sebastien.Varrette@uni.lu>
################################################################################
# Enable this completion file by adding somewhere in your zsh configuration (most
# probably ~/.zshrc):
#     fpath=(/path/to/this/dir $fpath)
# Test modification in live in your shell:
#
#  unfunction _mkdocs && autoload -U _mkdocs
#
typeset -A opt_args

# Main dispatcher
_mkdocs()
{
    local -a _mkdocs_cmds
    _mkdocs_cmds=(
        'build:Build the MkDocs documentation'
        'gh-deploy:Deploy your documentation to GitHub Pages'
        'json:Build the MkDocs documentation to JSON files...'
        'new:Create a new MkDocs project'
        'serve:Run the builtin development server'
    )
    local curcontext="$curcontext" state line ret=1
    typeset -A opt_args
    _arguments -C \
               '(- *)'{-h,--help}'[show this help message and exit]' \
               '(- *)'{--version,-V}"[Show the version and exit]" \
               '(- *)'{-q,--quiet}"Silence warnings" \
               '(- *)'{-v,--verbose}'[Enable verbose output]' \
               ': :->command' \
               '*:: :->options' && ret=0
    case $state in
        (command)
            _describe -t commands 'mkdocs' _mkdocs_cmds && ret=0
            ;;
        (options)
            curcontext=${curcontext%:*}-$line[1]:
            case $line[1] in
                (build) ret=0;;
                (serve) __mkdocs_serve && ret=0;;
                (new) _arguments -C \
                {-h,--help}'[show this help message and exit]' \
                ":dir:_directories" && ret=0;;
            esac
            ;;
    esac
    return ret
}

## Run the builtin development server
__mkdocs_serve() {
  local curcontext=$curcontext state line ret=1
  declare -A opt_args
  _arguments -s -S \
     {-h,--help}'[show this help message and exit]:' \
     {-q,--quiet}"[Silence warnings]:" \
     {-v,--verbose}'[Enable verbose output]' \
     {-s,--strict}'[Enable strict mode. This will cause MkDocs to abort the build on any warnings.]' \
     '--theme[The theme to use when building your documentation]:theme:(readthedocs mkdocs)' \
     '--livereload[Enable the live reloading in the development server (this is the default)]' \
     '--no-livereload[Disable the live reloading in the development server]' \
     '--dirtyreload[Enable the live reloading in the development server, but only re-build files that have changed]' \
     '--dev-addr[IP address and port to serve documentation locally]:host:(localhost\:8000 localhost\:8080)' \
     "*::args:_nothing"
}

_mkdocs "$@"
